package com.zenitera.bigdata.sink;


import com.alibaba.fastjson.JSON;
import com.zenitera.bigdata.bean.WaterSensor;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.ArrayList;

public class Flink01_Sink_Kafka01 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 50));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));

        DataStreamSource<WaterSensor> streamSource = env.fromCollection(waterSensors);
        streamSource
                .keyBy(WaterSensor::getId)
                .sum("vc")
                .map(JSON::toJSONString)
                .addSink(new FlinkKafkaProducer<String>("hadoop102:9092", "Flink01_Sink_Kafka", new SimpleStringSchema()));

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}


/*
[wangting@wangtingflink01 ~]$ kafka-topics.sh --bootstrap-server wangtingflink01:9092 --create --partitions 1 --replication-factor 3 --topic Flink01_Sink_Kafka
Created topic Flink01_Sink_Kafka.
[wangting@hadoop102 ~]$ kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic Flink01_Sink_Kafka
...
{"id":"sensor_1","ts":1607527992000,"vc":20}
{"id":"sensor_1","ts":1607527992000,"vc":70}
{"id":"sensor_1","ts":1607527992000,"vc":120}
{"id":"sensor_2","ts":1607527993000,"vc":10}
{"id":"sensor_2","ts":1607527993000,"vc":40}
 */